namespace EFS.Common.Functions
{
	public static class String
	{
		public static System.String GetTrimmedString(System.String variable)
		{
			if (null != variable)
			{
				if (variable.Length > 0)
				{
					return variable.Trim();
				}
				else
				{
					return "";
				}
			}

			return null;
		}

		public static System.String GetLowerTrimmedString(System.String variable)
		{
			System.String str = GetTrimmedString(variable);
			if (null != str)
			{
				return str.ToLower();
			}

			return null;
		}

		public static System.String GetUpperTrimmedString(System.String variable)
		{
			System.String str = GetTrimmedString(variable);
			if (null != str)
			{
				return str.ToUpper();
			}

			return null;
		}

		// first chat to Uppercase
		public static System.String GetFirsToUpper(System.String variable)
		{
			if (EFS.Common.Verify.String(variable))
			{
				variable = variable[0].ToString().ToUpper() + variable.Substring(1, variable.Length - 1);
			}

			return variable;
		}

		public static System.String GetTrimmedStringEntireWord(
			System.String value,
			System.Int32 maxCharsNumber)
		{
			// test value
			if (!EFS.Common.Verify.String(value))
			{
				return value;
			}

			// test length
			if (value.Length <= maxCharsNumber)
			{
				return value;
			}

			// last space index
			System.Int32 spaceIndex = value.IndexOf(' ', maxCharsNumber);
			if (-1 == spaceIndex)
			{
				return value;
			}

			// ok
			return value.Substring(0, spaceIndex);
		}

		/// Returns text in HTML format
		/**
		 *\param text The text to be formated
		 */
		public static System.String GetHTMLFormated(System.String text)
		{
			// check null
			if (null == text)
			{
				return null;
			}

			// check string
			if (0 == text.Length)
			{
				return text;
			}

			// string builder
			System.Text.StringBuilder sb =
				new System.Text.StringBuilder(text);

			// tabs -> triple spaces
			sb.Replace("\t", "&nbsp; &nbsp;");

			// double white spaces -> single white space and &nbsp;
			sb.Replace("   ", "&nbsp; &nbsp;");
			sb.Replace("  ", " &nbsp;");

			// brackets
			sb.Replace("<", "&lt;");
			sb.Replace(">", "&gt;");

			// quote
			sb.Replace("\"", "&quot;");

			// apos
			sb.Replace("'", "&apos;");

			// read lines
			System.IO.StringReader sr = new System.IO.StringReader(sb.ToString());
			System.IO.StringWriter sw = new System.IO.StringWriter();
			while (sr.Peek() > -1)
			{
				if (sw.GetStringBuilder().Length > 0)
				{
					sw.Write("<br />");
				}
				sw.Write(sr.ReadLine());
			}

			// Return the final processed text
			return sw.GetStringBuilder().ToString();
		}


		/// <summary>
		/// Split postal code
		/// </summary>
		/// <param name="postalCode"></param>
		/// <returns></returns>
		public static System.String GetAreaCodeFromPostalCode(System.String postalCode)
		{
			// check
			EFS.Common.Verify.String(postalCode, "postalCode");

			// sb
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			// get first letters
			foreach (System.Char c in postalCode)
			{
				if (System.Char.IsLetter(c))
				{
					sb.AppendFormat("{0}", c);
				}
				else
				{
					break;
				}
			}

			// get it
			return sb.ToString();
		}

		// append value with separator
		public static void AppendToStringBuilder(System.Text.StringBuilder stb, System.String valStr, System.String separator)
		{
			if (null == stb)
			{
				return;
			}

			if (EFS.Common.Verify.String(valStr))
			{
				if (stb.Length > 0)
				{
					stb.Append(separator);
				}
				stb.Append(valStr);
			}
		}
	}

}